package cn.itedus.lottery.domain.strategy.service.algorithm.impl;

import cn.itedus.lottery.domain.strategy.service.algorithm.BaseDrawAlgorithm;
import org.springframework.stereotype.Component;

import java.security.SecureRandom;
import java.util.List;

/**
 * 【推荐】单项随机概率抽奖，抽到一个已经排除掉的奖品则未中奖
 *
 * @author 闫佳成
 * @version: 1.0
 * @since 2024/10/28 14:27
 */
@Component("singleRateRandomDrawAlgorithm")
public class SingleRateRandomDrawAlgorithm extends BaseDrawAlgorithm {
    @Override
    public String randomDraw(Long strategyId, List<String> excludeAwardIds) {
        //获取策略对应元组
        String[] rateTuple = super.rateTupleMap.get(strategyId);
        assert rateTuple != null;

        //随机索引
        int randomVal = new SecureRandom().nextInt(100) + 1;
        int idx = super.hashIdx(randomVal);

        //返回结果
        String awardId = rateTuple[idx];
        if (excludeAwardIds.contains(awardId)) return null;
        return awardId;
    }
}
